package basis;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Scanner;

public class Demo9_Base64 {

	public static void main(String[] args) throws Exception {
		System.out.println("请输入你要编码的字符串");
		Scanner sc = new Scanner(System.in);
		String s=sc.next();
		System.out.println(encodeBase64(s.getBytes()));
		System.out.println("请输入你要解码的字符串");
		Scanner sc1 = new Scanner(System.in);
		String s1=sc1.next();
		System.out.println(new String(decodeBase64(s1)));
	}
	
	
	//Base64编码
	public static String encodeBase64(byte[]input) throws Exception{ 
	    Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); 
	    Method mainMethod= clazz.getMethod("encode", byte[].class); 
	    mainMethod.setAccessible(true); 
	    Object retObj=mainMethod.invoke(null, new Object[]{input}); 
	    return (String)retObj; 
		  } 
	
	
	//Base64解码
	public static byte[] decodeBase64(String input) throws Exception{ 
	    Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); 
	    Method mainMethod= clazz.getMethod("decode", String.class); 
	    mainMethod.setAccessible(true); 
	    Object retObj=mainMethod.invoke(null, input); 
	    return (byte[])retObj; 
	  } 

}
